package com.magiccompass.alipay.barcode.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.magiccompass.alipay.barcode.entity.MerchantTransaction;
import com.magiccompass.alipay.barcode.entity.MerchantTransactionDetail;
import com.magiccompass.alipay.barcode.entity.ProductInfo;
import com.magiccompass.alipay.barcode.pageform.MerchantInputProductPageForm;
import com.magiccompass.alipay.barcode.pageform.MerchantInputProductPageFormDetail;
import com.magiccompass.alipay.barcode.service.MerchantBusinessService;
import com.magiccompass.alipay.barcode.service.MerchantProductInputService;
import com.magiccompass.alipay.barcode.util.MerchantTransactionUtil;

@Controller
@RequestMapping(value="/merchant_demo")
public class DemoMerchantProductInputController {
	@Resource(name="merchantProductInputService")
	private MerchantProductInputService mpiService;
	
	@Resource(name="merchantBusinessService")
	private MerchantBusinessService merchantBusinessService;
	 
	@RequestMapping(method = RequestMethod.GET)
    public ModelAndView home() {
        ModelAndView model = new ModelAndView("merchant_demo/home");
        return model;
    }
	
	@RequestMapping(value="/inputTransaction", method = {RequestMethod.GET,RequestMethod.POST})
    public String inputTransaction(Model model) {
		List<ProductInfo> allProductsList = mpiService.listAllProducts();		
		model.addAttribute("merchantInputProductPageForm", makeMerchantInputProductPageForm(allProductsList));
        return "merchant_demo/inputTransaction";
    }
	
	@RequestMapping(value="/inputTransactionComplete")
    public String inputTransactionComplete(@ModelAttribute("merchantInputProductPageForm") MerchantInputProductPageForm mipp,Model model,RedirectAttributes redirectAttributes) {
		try {
			MerchantTransaction mt = MerchantTransactionUtil.makeMerchantTransaction(mipp,mpiService,merchantBusinessService);
			merchantBusinessService.save(mt);
			redirectAttributes.addFlashAttribute("merchantTransactionId", mt.getId());
			return "redirect:selectPayment";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			model.addAttribute("message", "failed");
			return "common/result";
		}        
    }
	@RequestMapping(value="/selectPayment")
    public String selectPayment(@ModelAttribute("merchantTransactionId") String merchantTransactionId,Model model) {
		MerchantTransaction mt = merchantBusinessService.findMerchantTransactionById(new Integer(merchantTransactionId));
		MerchantInputProductPageForm mippf = makeMerchantInputProductPageForm(mt);
		model.addAttribute("merchantInputProductPageForm", mippf);
		return "merchant_demo/selectPayment";
    }
	
	@RequestMapping(value="/selectPaymentTest")
    public String selectPaymentTest(Model model) {
		MerchantTransaction mt = merchantBusinessService.findMerchantTransactionById(70);
		MerchantInputProductPageForm mippf = makeMerchantInputProductPageForm(mt);
		model.addAttribute("merchantInputProductPageForm", mippf);
		return "merchant_demo/selectPaymentTest";
    }
	
	private MerchantInputProductPageForm makeMerchantInputProductPageForm(List<ProductInfo> allProductsList){
		List<MerchantInputProductPageFormDetail> returnList = new ArrayList<MerchantInputProductPageFormDetail>();
		MerchantInputProductPageFormDetail mippfd;
		for(ProductInfo aProduct:allProductsList){
			mippfd = new MerchantInputProductPageFormDetail();
			mippfd.setProductId(aProduct.getId());
			mippfd.setProductName(aProduct.getProductName());
			mippfd.setProductPrice(aProduct.getProductPrice());
			returnList.add(mippfd);
		}
		MerchantInputProductPageForm mippf = new MerchantInputProductPageForm();
		mippf.setProductList4Page(returnList);
		return mippf;
	}
	
	private MerchantInputProductPageForm makeMerchantInputProductPageForm(MerchantTransaction mt){
		MerchantInputProductPageForm mippf = new MerchantInputProductPageForm();
		mippf.setProductList4Page(makeMerchantInputProductPageFormDetail(mt.getId()));
		mippf.setTotalPrice(mt.getTotalAmount());
		mippf.setMerchantTransactionId(mt.getId());
		return mippf;		
	}
	
	private List<MerchantInputProductPageFormDetail> makeMerchantInputProductPageFormDetail(Integer mtId){
		List<MerchantTransactionDetail> mtOrderDetails = merchantBusinessService.findMerchantTransactionOrderDetailById(mtId);
		MerchantInputProductPageFormDetail aMerchantInputProductPageFormDetail;
		List<MerchantInputProductPageFormDetail> returnSet = new ArrayList<MerchantInputProductPageFormDetail>();
		for(MerchantTransactionDetail aOrderDetail:mtOrderDetails){
			aMerchantInputProductPageFormDetail = new MerchantInputProductPageFormDetail();
			aMerchantInputProductPageFormDetail.setProductId(aOrderDetail.getProduct().getId());
			aMerchantInputProductPageFormDetail.setProductName(aOrderDetail.getProduct().getProductName());
			aMerchantInputProductPageFormDetail.setProductPrice(aOrderDetail.getProduct().getProductPrice());
			aMerchantInputProductPageFormDetail.setQuantity(aOrderDetail.getQuantity());
			aMerchantInputProductPageFormDetail.setRowTotalAmount(aMerchantInputProductPageFormDetail.getProductPrice().multiply(new BigDecimal(aOrderDetail.getQuantity())));
			returnSet.add(aMerchantInputProductPageFormDetail);
		}
		return returnSet;
	}
}